<?php

namespace api\store;

use think\Cache;
use think\Model;

class StoreModel extends Model
{
    protected $table = 'data';
    protected $connection = '';

    public function __construct()
    {
        $this->connection = config('database');
        $this->table = 'store_' . $this->connection['database'];

        $store_name = config('store');
        if (!empty($store_name)) $this->connection['database'] = $store_name;

        parent::__construct();

        $ret_name = serialize($this->connection);
        if (!Cache::get($ret_name)) {
            $this->db()->cache()->query("CREATE TABLE IF NOT EXISTS `{$this->table}` ( `id` INT (11) NOT NULL AUTO_INCREMENT, `n` VARCHAR (255) DEFAULT NULL, `v` text, PRIMARY KEY (`id`)) DEFAULT CHARSET = utf8;");
            Cache::set($ret_name, true);
        }
    }
}